<template>
  <page-wrap hideTab title="我的报告" back :showTop="vipLevel == 0 ? true : false" :dark="vipLevel == 0 ? true : false"
    :stayStill="vipLevel == 0 ? true : false" navLeft hideSafeArea>
    <RightsContent class="content" :list="list" :vipLevel="vipLevel" :loaded="loaded" :rightsLoaded="rightsLoaded"
      rightsText="报告" tipsText="没有更多报告了，去查看其他报告 >" emptyText="您还没有报告，快去查看报告吧 >" :rightsInfo="rightsInfo"
      @loadMore="loadMore" @itemClick="handleClick" @emptyTextClick="handleEmptyTextClick"
      @tipsClick="handleEmptyTextClick" />
  </page-wrap>
</template>

<script>
import RightsContent from "../components/RightsContent.vue";
import { getPersonReport, getCompanyPersonOne } from "@/api/modules/company";

export default {
  components: {
    RightsContent,
  },
  data() {
    return {
      personId: uni.getStorageSync("person-id").slice(2) || undefined,
      companyId: uni.getStorageSync("company-id"),
      navHeight: getApp().globalData.navHeight,
      vipLevel: 0, // 2: 标准版；3：专业 4：旗舰版
      list: [],
      loading: false,
      loaded: false,
      finished: false,
      rightsLoaded: false,
      rightsInfo: {},
      pageParams: {
        pageNo: 0,
        pageSize: 20,
      },
    };
  },
  mounted() {
    this.getRights();
    this.getData();
  },
  methods: {
    handleClick(item) {
      console.log(item);
      this.activity = item;
      uni.navigateTo({
        url: '/pagesSub/companyCenter/report/detail?id=' + item.activityIdCode
      })
      // this.openReport(item.fileUrl);
    },
    openReport(fileUrl) {
      uni.showLoading({
        title: "打开中",
      });
      uni.downloadFile({
        url: fileUrl || this.activity.fileUrl, // 文件的本身url
        filePath:
          wx.env.USER_DATA_PATH + "/" + this.activity.activityName + ".pdf", // 本地自定义的文件名
        success: function (res) {
          let filePath = res.filePath;
          uni.openDocument({
            filePath: filePath,
            success: function () {
              uni.hideLoading();
            },
            fail: function () {
              uni.hideLoading();
            },
          });
          uni.hideLoading();
        },
        fail: function (res) {
          uni.showLoading({
            title: res,
          });
        },
      });
    },
    getRights() {
      const params = {
        personId: this.personId,
        companyId: this.companyId || this.personId,
        rightsSign: "library",
        rightsType: "public",
      };
      getCompanyPersonOne(params)
        .then((res) => {
          if (res.code == 200) {
            this.rightsInfo = res.data;
            this.vipLevel = res.data.yltLevel;
            if (this.vipLevel) {
              uni.setNavigationBarColor({
                frontColor: "#ffffff",
                backgroundColor: "",
              });
            }
          }
        })
        .finally(() => {
          this.rightsLoaded = true;
        });
    },
    // 获取报告
    getData() {
      if (this.loading || this.finished) return;
      let params = {
        personId: this.personId,
        companyId: this.companyId || this.personId,
        pageNo: this.pageParams.pageNo,
        pageSize: this.pageParams.pageSize,
      };

      this.loading = true;

      getPersonReport(params)
        .then((res) => {
          if (res.code == 200) {
            this.list.push(...res.data);
            this.pageParams.pageNo++;
            if (this.pageParams.pageNo > res.pageparam.pages) {
              this.finished = true;
            }
          } else {
            uni.showToast({
              title: res.status_desc,
              icon: "none",
            });
          }
        })
        .finally(() => {
          this.loading = false;
          this.loaded = true;
        });
    },
    loadMore() {
      this.getData();
    },
    handleEmptyTextClick() {
      uni.reLaunch({
        url: "/pages/companyReport/index",
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.content {
  flex: 1;
  display: flex;
  flex-direction: column;

  ::v-deep .nodata {
    padding-top: 200rpx;
  }
}
</style>